<?php

class YimeTools
{
    public static function getPlusOneTop10CommodityArray()
    {
        $result = CommodityPlusOne_Model::getAllResultArray();

        $array = array();
        foreach ($result as $thisPlusOne) {
            if (!isset($array[$thisPlusOne->commodity_id]))
                $array[$thisPlusOne->commodity_id] = array('count' => 0, 'data' => $thisPlusOne);
            $array[$thisPlusOne->commodity_id]['count']++;
        }

        function _sort_($a, $b)
        {
            $a = $a['count'];
            $b = $b['count'];

            if ($a < $b)
                return 1;
            else if ($b > $a)
                return -1;
            else
                return 0;
        }

        usort($array, '_sort_');

        $array = array_slice($array, 0, 10);

        $thatCommodityArray = array();
        foreach ($array as $t) {
            $commodity=Commodity_Model::getCommodityById($t['data']->commodity_id);
            if($commodity){
                $thatCommodityArray[] =$commodity;
            }

        }
        return $thatCommodityArray;
    }


    public static function getLatestReleaseTop10CommodityArray()
    {
        $thatCommodityArray = Commodity_Model::getCommodityArray(10, 0, 'release_time');
        return $thatCommodityArray;
    }

    /**
     * @return array
     */
    public static function getCurrentSchool()
    {
        /** @var CI $CI */
        $CI =& get_instance();
        $schoolcode=$CI->input->cookie('schoolcode', TRUE);
        $schoolname=$CI->input->cookie('schoolname', TRUE);
        $schoolid=$CI->input->cookie('schoolid', TRUE);
        if(!$schoolcode){
            $schoolcode=0;
            $schoolname='全国高校';
            $schoolid=0;
        }
        $currentSchool = array('schoolcode'=>$schoolcode,'schoolname'=>$schoolname,'schoolid'=>$schoolid);
        return $currentSchool;
    }

    public static function closetags($html)
    {
        #put all opened tags into an array
        preg_match_all('#<([A-Z0-9]+)[^>/]*>#i', $html, $result);
        $openedtags = $result[1];
        //var_dump($result);
        #put all closed tags into an array
        preg_match_all('#</([A-Z0-9]+)>#i', $html, $result);
        //var_dump($result);
        $closedtags = $result[1];
        $len_opened = count($openedtags);
        # all tags are closed
        if (count($closedtags) == $len_opened) {
            return $html;
        }
        $openedtags = array_reverse($openedtags);
        # close tags
        for ($i = 0; $i < $len_opened; $i++) {
            if (!in_array($openedtags[$i], $closedtags)) {
                $html .= '</' . $openedtags[$i] . '>';
            } else {
                unset($closedtags[array_search($openedtags[$i], $closedtags)]);
            }
        }
        return $html;
    }

    public static function sanitize_html($body)
    {

        $body=self::closetags($body);

        $body = preg_replace("/(<div([^>]*)>)|(<\/div>)/isU","",$body);
        //$body = preg_replace("/(<span([^>]*)>)|(<\/span>)/isU","",$body);
        //$body = preg_replace("/(<center([^>]*)>)|(<\/center>)/isU","",$body);
        //$body = preg_replace("/(<h[0-9]([^>]*)>)|(<\/h[0-9]>)/isU","",$body);

        //$body = preg_replace("/(<table([^>]*)>)|(<\/table>)/isU", "", $body);
        //$body = preg_replace("/(<tr([^>]*)>)|(<\/tr>)/isU", "", $body);
        //$body = preg_replace("/(<td([^>]*)>)|(<\/td>)/isU", "", $body);
        //$body = preg_replace("/(<tbody([^>]*)>)|(<\/tbody>)/isU", "", $body);

        //$body = preg_replace("/style=['\"]*([^'\"]*)['\"]*/is", "", $body); //过滤style属性
        $body = preg_replace("/class=['\"]*([^'\"]*)['\"]*/is", "", $body); //过滤class属性
        $body = preg_replace("/onload=['\"]*([^'\"]*)['\"]*/is", "", $body); //过滤class属性
        $body = preg_replace("/hspace=['\"]*([^'\"]*)['\"]*/is", "", $body); //过滤class属性
        $body = preg_replace("/vspace=['\"]*([^'\"]*)['\"]*/is", "", $body); //过滤class属性
        $body = preg_replace("/id=['\"]*([^'\"]*)['\"]*/is", "", $body); //过滤class属性


        $body = preg_replace("/<\!--.*?-->/si", "", $body); //注释
        $body = preg_replace("/<(\!.*?)>/si", "", $body); //过滤DOCTYPE
        $body = preg_replace("/<(\/?html.*?)>/si","",$body); //过滤html标签
        $body = preg_replace("/<(\/?head.*?)>/si","",$body); //过滤head标签
        $body = preg_replace("/<(\/?meta.*?)>/si","",$body); //过滤meta标签
        $body = preg_replace("/<(\/?body.*?)>/si","",$body); //过滤body标签
        $body = preg_replace("/<(\/?link.*?)>/si", "", $body); //过滤link标签
        $body = preg_replace("/<(\/?form.*?)>/si", "", $body); //过滤form标签
        $body = preg_replace("/cookie/si", "COOKIE", $body); //过滤COOKIE标签

        $body = preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si", "", $body); //过滤applet标签
        $body = preg_replace("/<(\/?applet.*?)>/si", "", $body); //过滤applet标签

        $body = preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si", "", $body); //过滤style标签
        $body = preg_replace("/<(\/?style.*?)>/si", "", $body); //过滤style标签

        $body = preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si", "", $body); //过滤title标签
        $body = preg_replace("/<(\/?title.*?)>/si", "", $body); //过滤title标签

        // $body = preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$body); //过滤object标签
        // $body = preg_replace("/<(\/?objec.*?)>/si","",$body); //过滤object标签

        $body = preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si", "", $body); //过滤noframes标签
        $body = preg_replace("/<(\/?noframes.*?)>/si", "", $body); //过滤noframes标签

        $body = preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si", "", $body); //过滤frame标签
        $body = preg_replace("/<(\/?i?frame.*?)>/si", "", $body); //过滤frame标签

        // $body = preg_replace("/<(embed.*?)>(.*?)<(\/embed.*?)>/si","",$body); //过滤embed标签
        // $body = preg_replace("/<(\/?embed.*?)>/si","",$body); //过滤embed标签

        $body = preg_replace("/<(ins.*?)>(.*?)<(\/ins.*?)>/si", "", $body); //过滤ins标签
        $body = preg_replace("/<(\/?ins.*?)>/si", "", $body); //过滤ins标签

        $body = preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si", "", $body); //过滤script标签
        $body = preg_replace("/<(\/?script.*?)>/si", "", $body); //过滤script标签
        $body = preg_replace("/javascript/si", "Javascript", $body); //过滤script标签
        $body = preg_replace("/vbscript/si", "Vbscript", $body); //过滤script标签
        $body = preg_replace("/on([a-z]+)\s*=/si", "On\\1=", $body); //过滤script标签
        $body = preg_replace("/&#/si", "&＃", $body); //过滤script标签，如javAsCript:alert('aabb)

        $body = preg_replace("/position: ?absolute/si", "", $body); //absolute属性




        return $body;


    }

} 